<?php

namespace App\Http\Controllers\Admin;

use App\Models\Menu;
use App\Models\RoleMenu;
use App\Models\User;
use App\Models\UserBuyer;
use App\Models\UserDetail;
use App\Models\UserSeller;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Validation\Rule;

class AuditController extends Controller
{


    /**
     * 列表页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function buyIndex()
    {


        return view('admin/audit/buy-index');
    }

    /**
     * 列表页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function sellerIndex()
    {


        return view('admin/audit/seller-index');
    }

    /**
     * 创建页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function add()
    {
        $menu = Menu::where('p_id', 0)->get();
        return view('admin/user/add', ['menu' => $menu]);
    }

    /**
     * 编辑页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function buyEdit(Request $request)
    {
        $params = $request->only(['id']);
        $validator = \Validator::make($params, [
            'id' => 'required',
        ], [
            'id.*' => '参数错误',
        ]);
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        $user = UserBuyer::find($request->id);
        return view('admin/audit/buy-edit', ['data' => $user]);
    }

    /**
     * 编辑页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function sellerEdit(Request $request)
    {
        $params = $request->only(['id']);
        $validator = \Validator::make($params, [
            'id' => 'required',
        ], [
            'id.*' => '参数错误',
        ]);
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        $user = UserSeller::find($request->id);
        return view('admin/audit/seller-edit', ['data' => $user]);
    }

    /**
     * 用户管理
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function buyData(Request $request)
    {
        $searchParams = json_decode($request->searchParams, true);
//        $phone = $searchParams['phone'] ?? '';
//        $name = $searchParams['name']??'';
//        $sort = $searchParams['sort']??'';
        $data = UserBuyer::from('user_buyer')
            ->where('user_buyer.status', 1)
            ->select('users.phone', 'user_buyer.*')
            ->leftJoin('users', 'users.id', '=', 'user_buyer.user_id')
            ->orderBy('id')
            ->paginate($request->limit)->toArray();
        return $this->success(['data' => $data['data'], 'count' => $data['total'], 'code' => 0]);

    }

    /**
     * 用户管理
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function sellerData(Request $request)
    {
        $searchParams = json_decode($request->searchParams, true);


        $data = UserSeller::select('users.phone', 'user_seller.*')
            ->where('user_seller.status', 1)
            ->leftJoin('users', 'users.id', '=', 'user_seller.user_id')
            ->orderBy('user_seller.id')
            ->paginate($request->limit)->toArray();

        return $this->success(['data' => $data['data'], 'count' => $data['total'], 'code' => 0]);

    }

    /**
     * 用户管理
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function data(Request $request)
    {
        $searchParams = json_decode($request->searchParams, true);
        $phone = $searchParams['phone'] ?? '';
        $name = $searchParams['name'] ?? '';
        $sort = $searchParams['sort'] ?? '';
        $data = UserDetail::from('user_detail')
            ->slect('users.phone', 'user_detail.*')
            ->leftJoin('users', 'users.id', '=', 'user_detail.user_id')
            ->orderBy('id')
            ->paginate($request->limit)->toArray();
        return $this->success(['data' => $data['data'], 'count' => $data['total'], 'code' => 0]);
        return $this->success($data);
    }

    /**
     * 用户信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function info(Request $request)
    {
        $params = $request->only(['user_id']);
        $validator = \Validator::make($params, [
            'user_id' => 'required|exists:users,id',
        ], [
            'user_id.*' => '参数错误',
        ]);
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        $user = User::find($request->user_id);

        return $this->success($user);

    }

    /**
     * 买家审核
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function saveBuy(Request $request)
    {
        $params = $request->only(['message', 'status', 'id']);

        $validator = \Validator::make($params, [
            'id' => 'required|exists:user_buyer,id',
            'status' => ['required', Rule::in([1,2,3])],

        ], [
            'id.*' => '参数错误',
            'status.*' => '状态参数错误',
        ]);
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        if( $request->status == 3 && $request->message == ''){
            return $this->error('请输入拒绝原因');
        }
        $id = $request->id;
        $user = UserBuyer::find($id);
        //修改身份
        if($request->status == 2) {
            $userMain = User::find($user->user_id);
            $userMain->is_buyers = 1;
            $userMain->save();
        }

        $user->status = $request->status;
        $user->message = $request->message;
        if ($user->save()) {
            return $this->success(['msg' => '修改成功']);
        }
        return $this->error('修改失败');

    }

    /**
     * 买家审核
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function saveSeller(Request $request)
    {
        $params = $request->only(['message', 'status', 'id']);

        $validator = \Validator::make($params, [
            'id' => 'required|exists:user_buyer,id',
            'status' => ['required', Rule::in([1,2,3])],

        ], [
            'id.*' => '参数错误',
            'status.*' => '状态参数错误',
        ]);
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        if( $request->status == 3 && $request->message == ''){
            return $this->error('请输入拒绝原因');
        }
        $id = $request->id;
        $user = UserSeller::find($id);
        //修改身份
        if($request->status == 2) {
            $userMain = User::find($user->user_id);
            $userMain->is_seller = 1;
            $userMain->save();
        }
        $user->status = $request->status;

        $user->message = $request->message;
        if ($user->save()) {
            return $this->success(['msg' => '修改成功']);
        }
        return $this->error('修改失败');
    }


}
